﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using NASA.BeAMartian.Controls;
using System.Windows.Navigation;
using System.Windows.Media.Imaging;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using NASA.BeAMartian.Model;
using Microsoft.TownHall.ServiceClient;

namespace NASA.BeAMartian
{
    public partial class Welcome : PhoneApplicationPage
    {
        public Welcome()
        {
            InitializeComponent();
            lbMenu.ItemsSource = new AppAreas();
            Loaded += new RoutedEventHandler(Welcome_Loaded);
        }

        void Welcome_Loaded(object sender, RoutedEventArgs e)
        {
            if (ApplicationContext.Instance.AppStarted == false)
            {
                ApplicationContext.Instance.AppStarted = true;
                (Resources["sbFadeIn"] as Storyboard).Begin();
            }
            else
            {
                grid.Opacity = 1.0;
                lightBeams.Opacity = 0.4;
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (ApplicationContext.Instance.UserData == null || ApplicationContext.Instance.UserData.UserID == 0)
            {
                tbLogin.Visibility = System.Windows.Visibility.Visible;
                imgAvatar.Visibility = System.Windows.Visibility.Collapsed;
                tbUsername.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                tbLogin.Visibility = System.Windows.Visibility.Collapsed;
                imgAvatar.Visibility = System.Windows.Visibility.Visible;
                imgAvatar.Source = new BitmapImage(new Uri(String.Format(
                    "http://beamartian.jpl.nasa.gov/s/img/avatars/avatar-{0}-pp-sm.png",
                    ApplicationContext.Instance.UserData.Avatar), UriKind.Absolute));
                tbUsername.Visibility = System.Windows.Visibility.Visible;
                tbUsername.Text = ApplicationContext.Instance.UserData.DisplayName.Replace("\r\n", "");
                 
            }
        }

        private void ListBox_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            FrameworkElement tb = e.OriginalSource as FrameworkElement;

            if (tb == null || tb.Tag == null)
                return;

            switch (tb.Tag.ToString())
            {
                case "view":
                    this.NavigationService.Navigate(new Uri("/Views/View.xaml?x=1", UriKind.Relative));
                    break;
                case "explore":
                    this.NavigationService.Navigate(new Uri("/Views/Explore/MarsAtlas.xaml?x=1", UriKind.Relative));
                    break;
                case "participate":
                    this.NavigationService.Navigate(new Uri("/Views/Participate/Index.xaml", UriKind.Relative));
                    break;
                case "learn":
                    this.NavigationService.Navigate(new Uri("/Views/Learn/Index.xaml?x=0", UriKind.Relative));
                    break;

            }
        }

        private void Image_Loaded(object sender, RoutedEventArgs e)
        {
            Image img = sender as Image;
            img.Source = new BitmapImage(new Uri(
                String.Format("Assets/Images/icons/ico_{0}.png", img.Tag), UriKind.RelativeOrAbsolute));
            
        }

        private void tbLogin_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Views/SignIn.xaml", UriKind.Relative));
        }

        private void tbAbout_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Views/About.xaml", UriKind.Relative));
        }

        private void user_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Views/Profile.xaml", UriKind.Relative));
        }
    }
}